import smtplib
import datetime
import calendar
from email.message import EmailMessage


def add_exchange_abbrev(stockid: str):
    """
    根据沪深开头代码的区别，在股票代码前面加上"sh"或者"sz"
    """
    SH = {
        '60', # 股票
        '50', '51', # 基金
        '11' # 可转债
    }
    if stockid[:2] in SH:
        return 'sh'+stockid
    else:
        return 'sz'+stockid


def send_mail_to(receivers, header, body, host, port, username, password):
    smtp = smtplib.SMTP()
    smtp.connect(host, port)
    smtp.login(username, password)
    for receiver in receivers:
        msg = EmailMessage()
        msg['Subject'] = header
        msg['From'] = f"{username} <{username}>"
        msg['To'] = receiver
        msg.set_type("text/plain")
        msg.set_content(body)
        smtp.send_message(msg)


def is_during_trade_time():
    now = datetime.datetime.now()
    if now.weekday() < 6 and now.hour >= 9 and now.hour <= 15:
        return True
    return False
